/*
 * hooc.c
 *
 *  Created on: Nov 22, 2011
 *      Author: benoybose
 */


#include "hxmain.h"
#include "hcstrstream.h"


extern FILE* yyin;
extern int yyparse();

const char* HSOURCELIST = "source_list";

int main(int argc, char** argv) {

    LOGINIT("hooc.log");
    hxcmdline_options* options = hxcmdline_create_options();
    if (0 < cmdline_parse(argc, argv, options)) {
	LOG_ERROR(MOD_HOOC, "Invalid user options.");
	return (1);
    }

    hclist* srclist = resolve_source_objects(options);
    if ((0 != srclist) && (0 < hclist_count(srclist))) {
	hclist_foreach(srclist, foreach_through_src);
    }

    return (0);
}

hclist* resolve_source_objects(hxcmdline_options* options) {
    int inputfile_count = hxcmdline_count_inputfiles(options);
    if (0 < inputfile_count) {
	hclist* list = hclist_create(HSOURCELIST);
	int index = 0;
	for (index = 0; index < inputfile_count; index++) {
	    char* filename = hxcmdline_get_inputfile(options, index);
	    char* fileext = hxcmdline_get_inputfile_ext(options, index);
	    hcsrcobj* pSourceObject = hcsrcobj_create(filename, fileext);
	    if (0 != pSourceObject) {
		hclist_add(list, (void*) pSourceObject);
	    } else {
		printf(FILEERROR2, filename);
	    }
	}
	return list;
    } else {
	return 0;
    }
}

void foreach_through_src(void* src) {
    hcsrcobj* item = (hcsrcobj*) src;
    if (0 != item) {
	int result = hccompiler_compile(src);
	if (0 != result) {
	    printf("Compilation of %s failed. error code: %d\n",
		    item->filename->text,
		    result);
	}
    }
    return;
}